<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #outer {
            overflow: hidden;
            position: relative;
            width: 355px;
            height: 168px;
            margin: 20px auto;
            background-color: pink;
            padding: 10px 0;
        }

        #imgList {
            position: absolute;
            list-style: none;
            /* width: 1775px; */
            /* left: -355px; */
        }

        #imgList li {
            float: left;
            margin: 0 10px;
        }

        img {
            width: 335px;
            height: 168px;
        }

        #nav {
            position: absolute;
            bottom: 10px;
            left: 50%;
            transform: translateX(-50%);
        }

        #nav a {
            display: block;
            float: left;
            width: 12px;
            height: 12px;
            background-color: white;
            margin-right: 10px;
            /* opacity透明度 */
            opacity: 0.5;
        }

        #nav a:hover {
            opacity: 1;
        }
    </style>
    <script src="tools.js"></script>
    <script>
        window.onload = function () {
            let imgList = document.getElementById("imgList");
            let imgArr = document.getElementsByTagName("img");
            let allA = document.getElementsByTagName("a");

            imgList.style.width = 355 * imgArr.length + "px";
            let index = 0;
            allA[index].style.opacity = 1;
            
            let autoChange;
            swiper();
            for (let i = 0; i < allA.length; i++) {
                allA[i].num = i; //给a标签添加属性num作为新的索引,各个超链接之间互不影响

                allA[i].onclick = function () {
                    //点击后取消自动切换
                    clearInterval(autoChange);
                    index = this.num;
                    // imgList.style.left = (-355 * index) + "px"; 
                    setA();
                    move(imgList, "left", 10, (-355 * index), function () {
                        //动画执行后开启自动切换
                        swiper();
                    });
                }
            }
            function setA(){
                if(index >= imgArr.length - 1) {
                    index = 0;
                    // 通过css把最后一张切换成第一张(瞬间/障眼法)
                    imgList.style.left = 0;

                }
                for (let j = 0; j < allA.length; j++) {
                        allA[j].style.opacity = null;
                        //js中设置的样式为内联样式，优先级高，覆盖hover，设空则不影响 
                    }
                    allA[index].style.opacity = 1;
            }
            function swiper() {
                    autoChange = setInterval(() => {
                    index++;
                    index %= imgArr.length;
                    move(imgList, "left", 10, -355*index, function () {setA();});
                }, 2000);
            }
        }
    </script>
</head>

<body>
    <div id="outer">
        <ul id="imgList">
            <li><img src="images/1/1.jpg" alt=""></li>
            <li><img src="images/1/2.jpg" alt=""></li>
            <li><img src="images/1/3.jpg" alt=""></li>
            <li><img src="images/1/4.jpg" alt=""></li>
            <li><img src="images/1/5.jpg" alt=""></li>
            <li><img src="images/1/1.jpg" alt=""></li>
        </ul>
        <div id="nav">
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>

        </div>
    </div>
</body>

</html>